require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'


class TC_OpenId_test < Test::Unit::TestCase
  
    def startup
      $UserName ='User123'
      $UserPassword ='pass123'
      $UserEmail = 'sajid.greatdeccan@gmail.com'
      $UserOpenId = 'http://sajid.greatdeccan.myopenid.com/'
      $OpenIdPassword  = 'aries1182'
      @@ie = Watir::IE.start($SITE_URL)
    end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
    
    def test_01_verifyHomeText
      assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
       
    def test_03_joinAsNewUser
      ie.link(:text,'Join').click
      sleep 2
      assert(ie.text_field(:id,/Username$/).exists?)
      ie.text_field(:id,/Username$/).value = $UserName
      assert(ie.text_field(:id,/Password$/).exists?)
      ie.text_field(:id,/Password$/).value = $UserPassword
      assert(ie.text_field(:id,/Password2$/).exists?)
      ie.text_field(:id,/Password2$/).value =$UserPassword
      assert(ie.text_field(:id,/Email$/).exists?)
      ie.text_field(:id,/Email$/).value = $UserEmail
      assert(ie.text_field(:id,/Email2$/).exists?)
      ie.text_field(:id,/Email2$/).value = $UserEmail
      assert(ie.select_list(:id,/Timezone$/).exists?)
      ie.select_list(:id,/Timezone$/).select('(GMT +05:00) Islamabad, Karachi, Tashkent')
      assert(ie.button(:name,/CreateAccount$/).exists?)
      ie.button(:name,/CreateAccount$/).click
    end
    
    def test_04_associateOpenId
      ie.link(:text , 'Home').click
      sleep 2
      ie.link(:text , 'Edit profile').click
      sleep 2
      ie.ie.Document.parentWindow.execScript("ctl00_bcr_EditUserForm1_ctl02_ProfileTabs.SelectTab(ctl00_bcr_EditUserForm1_ctl02_ProfileTabs.GetTabAtIndex(2))")
      assert(ie.text_field(:id,/openId$/).exists?)
      ie.text_field(:id,/openId$/).value =$UserOpenId
      assert(ie.button(:name,/AddOpenId$/).exists?)
      ie.button(:name,/AddOpenId$/).click
      sleep 5 
      if(ie.text_field(:id,'password').exists?)
       ie.text_field(:id,'password').value =$OpenIdPassword
       sleep 4
       assert(ie.button(:id,'signin_button').exists?)
       ie.button(:id,'signin_button').click
       sleep 3
     end
     sleep 2
     ie.ie.Document.parentWindow.execScript("ctl00_bcr_EditUserForm1_ctl02_ProfileTabs.SelectTab(ctl00_bcr_EditUserForm1_ctl02_ProfileTabs.GetTabAtIndex(2))")
     assert(ie.button(:id,/UpdateButtonBottom$/).exists?)
     ie.button(:id,/UpdateButtonBottom$/).click
  end
  
 def test_05_testOpenId
    test_02_verifyLoginLinksExist
    sleep 2
    ie.link(:text , 'Sign in').click
    sleep 2
    ie.link(:text , 'OpenID').click
    sleep 2
    ie.text_field(:id,/openId$/).value = $UserOpenId
    sleep 2
    ie.button(:id,/loginOpenIdButton$/).click
    sleep 2   
  end
  
  def  test_06_searchUser
    test_02_verifyLoginLinksExist
    ie.link(:text, 'Sign in').click
    assert(ie.contains_text("Sign in name"))
    ie.text_field(:id, /username$/).value = $ADMIN_USER
    assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
    ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
    assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
    ie.button(:id, /loginButton$/).click
    sleep 5
    ie.goto($SITE_URL +'controlpanel/membership/membersearch.aspx?SelectedNavItem=BrowseMembers')
    assert(ie.text_field(:id,/searchText$/).exists?)
    ie.text_field(:id,/searchText$/).value ="*"
    assert(ie.select_list(:name,/PageList$/).exists?)
    ie.select_list(:name,/PageList$/).select('10')
    sleep 3
    assert(ie.button(:id,/searchButton$/).exists?)
    sleep 2
    ie.button(:id,/searchButton$/).click
  end
     
  def test_07_navigateToDeleteUser
    sleep 5
    table1 = ie.table(:index, 6)
    rowcount = table1.row_count
    rowcount1 =1
    ok =0
    while rowcount1<=rowcount
      if (table1[rowcount1][1].link(:text,$UserName).exists?)
       ok =1
       sleep 5
       table1[rowcount1][5].button(:value, 'Delete').click
       sleep 3
       break
       end   
      rowcount1=rowcount1+1
      end
     if(ok ==0)
      sleep 5
      ie.link(:text,'Next >').click
      sleep 5
      test_07_navigateToDeleteUser
      end
    sleep 5
    assert(ie.frame(:index,1).radio(:id,/AssignToAnonymous$/).exists?)
    ie.frame(:index,1).radio(:id,/AssignToAnonymous$/).set
    sleep 3
    assert(ie.frame(:index,1).button(:id,/DeleteUserButton$/).exists?)
    sleep 3
    ie.frame(:index,1).button(:id,/DeleteUserButton$/).click
    sleep 5
    ie.goto($SITE_URL +'controlpanel/membership/membersearch.aspx?SelectedNavItem=BrowseMembers')
    assert(ie.text_field(:id,/searchText$/).exists?)
    ie.text_field(:id,/searchText$/).value ="*"
    sleep 3
    assert(ie.button(:id,/searchButton$/).exists?)
    sleep 2
    ie.button(:id,/searchButton$/).click  
    if(ie.link(:text,$UserName).exists?)
      puts 'User Not Deleted'
       end   
     end  
     
    def test_99_Exit
        ie.close
      end
      

  end
  